//
//  WZGAplyFillInformationVC.m
//  ZCJZ
//
//  Created by 潘洪波 on 2020/2/27.
//  Copyright © 2020 jeking. All rights reserved.
//

#import "WZGAplyFillInformationVC.h"
#import "WZGAplyFillInformationTwoCell.h"
#import "WZGAplyFillInfomationOneCell.h"
#import "WZGAfterAplyStatusVC.h"
#import "SKBaseWebService.h"
#import <LCActionSheet.h>
@interface WZGAplyFillInformationVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *mTableView ;

@property (nonatomic, strong) NSString *idcard_front ;
@property (nonatomic, strong) NSString *idcard_back ;
@property (nonatomic, strong) NSString *idcard_inhand ;
@property (nonatomic, strong) NSString *other_cert ;

@end


@implementation WZGAplyFillInformationVC

- (void)viewDidLoad{
    [super viewDidLoad];
    self.mTableView.delegate = self;
    self.mTableView.dataSource = self;
    self.mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mTableView.estimatedRowHeight = 100;
    
    self.view.backgroundColor = UIColorFromRGB(0xf5f5f5);
}

#pragma mark - UITableViewDataSource
#pragma mark -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf(self);
    switch (indexPath.section) {
        case 0:
        {
            WZGAplyFillInformationTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"twoCell"];
            if (!cell) {
                cell = [[WZGAplyFillInformationTwoCell alloc] initWithFlex:nil reuseIdentifier:@"twoCell"];
            }
            cell.forntImg = self.idcard_front;
            cell.backImg = self.idcard_back;
            
            cell.imageTaped = ^(NSInteger tag){
                [weakself chooseImageAction:^(NSString *url) {
                    if (tag == 100) {
                        weakself.idcard_front = url;
                    }else{
                        weakself.idcard_back = url;
                    }
                    dispatch_async(dispatch_get_main_queue(), ^{
                         [tableView reloadData];
                    });
                }];
            };
            return cell;
        }
            break;
        case 1:
        {
            WZGAplyFillInfomationOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneCell"];
            if (!cell) {
                cell = [[WZGAplyFillInfomationOneCell alloc] initWithFlex:nil reuseIdentifier:@"oneCell"];
            }
            cell.placeholderImg = [UIImage imageNamed:@"idcard_inhand"];
            cell.cerImg = self.idcard_inhand;
            cell.imageTaped = ^{
                [weakself chooseImageAction:^(NSString *url) {
                    weakself.idcard_inhand = url;
                    dispatch_async(dispatch_get_main_queue(), ^{
                         [tableView reloadData];
                    });
                }];
            };
            return cell;
        }
            break;
        case 2:
        {
            WZGAplyFillInfomationOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneCell"];
            if (!cell) {
                cell = [[WZGAplyFillInfomationOneCell alloc] initWithFlex:nil reuseIdentifier:@"oneCell"];
            }
            if (self.type == 2) {
                cell.title = @" 毕业证/学生证/在校证明";
                cell.placeholderImg = [UIImage imageNamed:@"idcard_xuesheng"];
            }else if ( self.type == 3){
                cell.title = @" 残疾人证照片或残障人士证明";
                cell.placeholderImg = [UIImage imageNamed:@"idcard_canji"];
            }
            cell.cerImg = self.other_cert;
            cell.imageTaped = ^{
                [weakself chooseImageAction:^(NSString *url) {
                    weakself.other_cert = url;
                    dispatch_async(dispatch_get_main_queue(), ^{
                         [tableView reloadData];
                    });
                }];
            };
            return cell;
        }
            break;

        default:
            return [UITableViewCell new];
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *v = [UIView new];
    v.backgroundColor = [UIColor clearColor];
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

#pragma mark - UITableViewDelegate
#pragma mark -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)confirmAction{
    if (!self.idcard_front || !self.idcard_back || !self.idcard_inhand || !self.other_cert) {
        [self.view makeToast:@"请填写完整信息"];
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.type == 2) {//大学生
        [param setValue:@"1" forKey:@"ios_channel"];
    }else{//残疾人
        [param setValue:@"2" forKey:@"ios_channel"];
    }
    WeakSelf(self);
    [param setValue:self.idcard_front forKey:@"id_card_front"];
    [param setValue:self.idcard_back forKey:@"id_card_back"];
    [param setValue:self.idcard_inhand forKey:@"id_card_inhand"];
    [param setValue:self.other_cert forKey:@"other_cert"];
    [SKBaseWebService postRequest:@"/v1/member-upgrade-apply/submit" parameters:param progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        if (status == kHXSNoError) {
            [weakself dismissViewControllerAnimated:YES completion:^{
                SYBaseViewController * curVC = (SYBaseViewController*)[generalToolBox getcurrentVC];
                WZGAfterAplyStatusVC *vc = [WZGAfterAplyStatusVC new];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
                nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [curVC presentViewController:nav animated:YES completion:nil];
            }];
        }
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        [weakself.view makeToast:msg];
    }];

}

- (void)chooseImageAction:(void(^)(NSString *url))complete{
    __weak typeof(self) weakself = self;
    LCActionSheet *sheet = [[LCActionSheet alloc] initWithTitle:nil cancelButtonTitle:@"取消" clicked:^(LCActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            [weakself showCamera:^(NSMutableArray *result) {
                if (complete) {
                    complete(result.firstObject);
                }
            }];
        }
        if (buttonIndex == 2) {
            weakself.tzvc = nil;
            [weakself showAlbum:^(NSMutableArray *result) {
                if (complete) {
                    complete(result.firstObject);
                }
            }];
        }
    } otherButtonTitles:@"拍照",@"相册选择", nil];
    sheet.buttonFont = [UIFont systemFontOfSize:15 weight:UIFontWeightThin];
    [sheet show];
}

@end
